﻿#region Header

/*
Remote views based on linq
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.Reflection;
using Sebarf.Utils.Interfaces;

#endregion

namespace Sebarf.Services.Interfaces {
	/// <summary>
	/// Description of the class
	/// </summary>
	public class ServiceStatistics {
		#region Public Properties

		private IObjectHierarchyFlattenerService ObjectHierarchyFlattenerService { set; get; }

		public IMonitorableService UnderlyingService { get; private set; }

		public String FriendlyServiceName { get; set; }

		public DateTime CollectedDate { get; private set; }

		public String ServiceName {
			get { return UnderlyingService.GetType().Name; }
		}

		public ServiceStatisticEntry[] ServiceStatisticEntries {
			get { return m_statisticEntries.ToArray(); }
		}

		public IServiceImpl[] ReferencedServices {
			get {
				var toReturn = new List<IServiceImpl>();
				foreach (PropertyInfo property in ObjectHierarchyFlattenerService.GetAllProperties(UnderlyingService.GetType())) {
					foreach (Attribute attribute in property.GetCustomAttributes(true)) {
						if (attribute is ServiceRequest) {
							var service = property.GetValue(UnderlyingService, null) as IServiceImpl;
							if (service != null && !toReturn.Contains(service)) {
								toReturn.Add(service);
							}
						}
					}
				}
				return toReturn.ToArray();
			}
		}

		#endregion

		#region Public Methods

		public ServiceStatistics(IMonitorableService service, IObjectHierarchyFlattenerService flattener) {
			UnderlyingService = service;
			CollectedDate = DateTime.Now;
			ObjectHierarchyFlattenerService = flattener;
		}

		public ServiceStatistics(IMonitorableService service, IList<ServiceStatisticEntry> entries,
								  IObjectHierarchyFlattenerService flattener)
			: this(service, flattener) {
			m_statisticEntries.AddRange(entries);
		}

		public void AddEntry(ServiceStatisticEntry entry) {
			m_statisticEntries.Add(entry);
		}

		public void AddEntry(string key, object value) {
			m_statisticEntries.Add(new ServiceStatisticEntry { CollectedValue = value, Key = key });
		}

		#endregion

		#region Private Fields

		private readonly List<ServiceStatisticEntry> m_statisticEntries = new List<ServiceStatisticEntry>();

		#endregion
	}

	public class ServiceStatisticEntry {
		public bool IsNumericEntry {
			get { return (CollectedValue is double); }
		}

		public Object CollectedValue { get; set; }
		public string Key { get; set; }
	}
}